package GUILayer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.* ;
import java.util.*;
import ControlLayer.*;



/**
 * Write a description of class UICopy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class UICopy
{
   
    private  JTextField idDVDText=new JTextField(10);
    private  JTextField serialCopyText = new JTextField(10);
    private  JTextField purchaseDateText = new JTextField(10);
    private  JTextField purchasePriceText=new JTextField(10);
    private  JTextField deleteCopySerialText=new JTextField(10);
    private  JTextField deleteDVDIDText=new JTextField(10);
    
    private DVDCtr dvdController;

    /**
     * Constructor for objects of class UICopy
     */
    public UICopy()
    {
      dvdController=new DVDCtr();
    }
    
     public JPanel getCopiesPanel(JFrame frame)
    {
        JPanel panelMain=new JPanel();
        panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
        
        JButton addCopy=new JButton("Add Copy");
        JButton deleteCopy=new JButton("Delete Copy");
        
        JLabel idDVDLabel= new JLabel("              ID DVD ");
        JLabel deleteDVDIDLabel=new JLabel("         ID DVD      ");
        JLabel deleteCopySerialLabel= new JLabel("Serial Number");
        JLabel serialCopyLabel= new JLabel("Serial Number");
        JLabel purchaseDateLabel= new JLabel("Purchase Date");
        JLabel purchasePriceLabel= new JLabel("Purchase Price");

        final JFrame frame1=frame;
        addCopy.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { addCopy(frame1); }
                           });  
        deleteCopy.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { deleteCopy(frame1); }
                           });         
         

        

        
        JPanel idDVDPanel=new JPanel();
        idDVDPanel.add(idDVDLabel);
        idDVDPanel.add(idDVDText);
        addComponent(idDVDPanel,panelMain);
        
        JPanel panelSerial=new JPanel();
        panelSerial.add(serialCopyLabel);
        panelSerial.add(serialCopyText);
        addComponent(panelSerial,panelMain);
        
        JPanel panelPurchaseDate=new JPanel();
        panelPurchaseDate.add(purchaseDateLabel);
        panelPurchaseDate.add(purchaseDateText);
        addComponent(panelPurchaseDate,panelMain);
        
        JPanel panelPurchasePrice=new JPanel();
        panelPurchasePrice.add(purchasePriceLabel);
        panelPurchasePrice.add(purchasePriceText);
        addComponent(panelPurchasePrice,panelMain);
        
                panelMain.add(Box.createRigidArea(new Dimension(300,5)));
        addComponent(addCopy,panelMain);
           panelMain.add(Box.createRigidArea(new Dimension(300,15)));
        

        
        JPanel panelToDeleteDVDID=new JPanel();
        panelToDeleteDVDID.add(deleteDVDIDLabel);
        panelToDeleteDVDID.add(deleteDVDIDText);
        addComponent(panelToDeleteDVDID,panelMain);
        
        JPanel panelDeleteCopySerial=new JPanel();
        panelDeleteCopySerial.add(deleteCopySerialLabel);
        panelDeleteCopySerial.add(deleteCopySerialText);
        addComponent(panelDeleteCopySerial,panelMain);
        
          
        addComponent(deleteCopy,panelMain);
         panelMain.add(Box.createRigidArea(new Dimension(300,15)));
        
        
         
         return panelMain;
    }
    
   
    private void addCopy(JFrame frame)
    {
        try
          {
           long id=Long.valueOf(idDVDText.getText()) ;
           long serial=Long.valueOf(serialCopyText.getText());
    
          dvdController.createCopy(id,serial,purchaseDateText.getText(),
           Integer.valueOf(purchasePriceText.getText()));
           idDVDText.setText("");
           serialCopyText.setText("");
           purchaseDateText.setText("");
           purchasePriceText.setText("");
       
          
          JOptionPane.showMessageDialog(frame,"Copy added successfuly.");
          }
          
          catch(Exception e)
          {
            JOptionPane.showMessageDialog(frame,"Error while adding copy,please try again!.","",JOptionPane.ERROR_MESSAGE);
            }
    
     }
    
    private void deleteCopy(JFrame frame)
    {
        try
        {
         long  id=Long.valueOf(deleteDVDIDText.getText()) ;
         long serial=Long.valueOf(deleteCopySerialText.getText());

         int choice=JOptionPane.showConfirmDialog(frame,"Do you really want to delete this Copy?", "", JOptionPane.YES_NO_OPTION);
           if(choice==0)
           {
           dvdController.deleteCopy(id,serial);
           deleteDVDIDText.setText("");
           serialCopyText.setText("");
           JOptionPane.showMessageDialog(frame,"Copy deleted successfuly.");
           }
        }
        
        catch(Exception e)
        {
          JOptionPane.showMessageDialog(frame,"Wrong ID ot Serial,please try again!.","",JOptionPane.ERROR_MESSAGE);
        }
    }
     
     
    private static void addComponent(JComponent component, Container container)
    {
        component.setAlignmentX(Component.CENTER_ALIGNMENT);
        container.add(component);
        
    }


}
